package com.irdstudio.tdp.executor.core.plugin.migrate.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 迁移校验规则类-[表名: 迁移校验规则(migrate_check_rule)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2020-11-04
 */
public class MigrateDictRelationDao {

	/* 连接对象 */
	Connection conn = null;

	public MigrateDictRelationDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateDictRelation> queryMigrateDictRelationWithAll()
			throws SQLException {
		List<MigrateDictRelation> dcList = new ArrayList<MigrateDictRelation>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		MigrateDictRelation dc = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM migrate_dict_relation");
			rs = ps.executeQuery();
			while (rs.next()) {
				dc = new MigrateDictRelation();
				dc.setRelationId(rs.getString("relation_id"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dc.setDictId(rs.getString("target_table_name"));
				dc.setDictCode(rs.getString("dict_code"));
				dc.setDictName(rs.getString("dict_name"));
				dc.setSourceFieldCode(rs.getString("source_field_code"));
				dc.setSourceFieldName(rs.getString("source_field_name"));
				dc.setTargetFieldCode(rs.getString("target_field_code"));
				dc.setTargetFieldName(rs.getString("target_field_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateCheckRuleWithAll is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateDictRelation> queryMigrateDictRelationWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<MigrateDictRelation> dcList = new ArrayList<MigrateDictRelation>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM migrate_dict_relation "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				MigrateDictRelation dc = new MigrateDictRelation();
				dc.setRelationId(rs.getString("relation_id"));
				dc.setSourceTableId(rs.getString("source_table_id"));
				dc.setSourceTableCode(rs.getString("source_table_code"));
				dc.setSourceTableName(rs.getString("source_table_name"));
				dc.setTargetTableId(rs.getString("target_table_id"));
				dc.setTargetTableCode(rs.getString("target_table_code"));
				dc.setTargetTableName(rs.getString("target_table_name"));
				dc.setDictId(rs.getString("target_table_name"));
				dc.setDictCode(rs.getString("dict_code"));
				dc.setDictName(rs.getString("dict_name"));
				dc.setSourceFieldCode(rs.getString("source_field_code"));
				dc.setSourceFieldName(rs.getString("source_field_name"));
				dc.setTargetFieldCode(rs.getString("target_field_code"));
				dc.setTargetFieldName(rs.getString("target_field_name"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryMigrateCheckRuleWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}

	/**
	 *
	 * @param sourceFieldCode
	 * @param targetFieldCode
	 * @return
	 * @throws SQLException
	 */
	public List<MigrateDictRelation> relationWhereString(StringBuffer sourceFieldCode,StringBuffer targetFieldCode) throws SQLException {
		String Relationwherestr="where source_field_code in" +sourceFieldCode.substring(0,sourceFieldCode.length() - 1)+")"
				+" and  target_field_code in "+targetFieldCode.substring(0,targetFieldCode.length() - 1)+")"
				;
		return queryMigrateDictRelationWithCond(Relationwherestr,"");
	}



	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
